package com.codeyang.service.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.demo.trade.model.builder.AlipayTradePrecreateRequestBuilder;
import com.alipay.demo.trade.model.builder.AlipayTradeQueryRequestBuilder;
import com.alipay.demo.trade.model.result.AlipayF2FPrecreateResult;
import com.alipay.demo.trade.model.result.AlipayF2FQueryResult;
import com.alipay.demo.trade.service.AlipayTradeService;
import com.codeyang.config.AliPayProperties;
import com.codeyang.domain.Order;
import com.codeyang.feign.PayOrderFeign;
import com.codeyang.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Map;

/**
 * 描述: describe
 *
 * @author CodeYang_Site
 * @version 2021/6/2 17:41
 */
@Service
public class PayServiceImpl implements PayService {


    @Autowired
    private PayOrderFeign payOrderFeign;

    /**
     * 阿里的支付服务
     */
    @Autowired
    private AlipayTradeService alipayTradeService;


    @Autowired
    private AliPayProperties aliPayProperties;


    /**
     * 根据订单号
     * 查询订单状态,生成二维码
     *
     * @param orderNumber
     * @return
     */
    @Override
    public String toPay(String orderNumber) {
        //1. 根据订单号 得到订单消息
        Order order = payOrderFeign.getOrderByOrderNumber(orderNumber);
        if (ObjectUtils.isEmpty(order)) {
            throw new RuntimeException("订单为空");
        }

        AlipayTradePrecreateRequestBuilder builder = new AlipayTradePrecreateRequestBuilder();
        builder.setOutTradeNo(orderNumber); //设置订单号
        builder.setTotalAmount(order.getTotal().toString());//设置总金额 从分开始
        builder.setSubject(order.getProdName());// 设置标题主题,商品内容
        builder.setOperatorId("test_op_id"); //设置操作员id
        builder.setStoreId("test_st_id");//设置门店id
        //这里的回调URL的绑定端口一定要与 支付模块的的端口一致
        // ip + 端口 +/ 通知接收的地址 payNotify 接收这个就知道有没有支付成功了
        builder.setNotifyUrl("http://alipay.cn.utools.club/payNotify");

        //以上几个参数,是设置创建交易订单最少的设置信息
        AlipayF2FPrecreateResult result = alipayTradeService.tradePrecreate(builder);
        switch (result.getTradeStatus()) {
            case SUCCESS:
                //支付成功
                //返回响应的二维码地址
                return result.getResponse().getQrCode();
            case FAILED:
            case UNKNOWN:
                return "失败";
            default:
                throw new RuntimeException("创建预订单失败");
        }

        //0.return 一个 支付宝的二维码地址
    }

    /**
     * 验签 验证是不是自己的发送的订单
     *
     * @param map
     * @return
     */
    @Override
    public Boolean checkPay(Map<String, String> map) {
        boolean checkV1 = false;
        try {
            checkV1 = AlipaySignature.rsaCheckV1(map, aliPayProperties.getPublicKey(), aliPayProperties.getCharset(), aliPayProperties.getSignType());

        } catch (AlipayApiException e) {
            e.printStackTrace();
            System.out.println("验签失败");
        }
        return checkV1;
    }

    /**
     * 修改订单状态 根据订单号 --交易单号
     *
     * @param orderNumber
     */
    @Override
    public void changeOrderStatus(String orderNumber) {
        payOrderFeign.changeOrderStatus(orderNumber);

    }

    /**
     * 查看订单交易状态
     * @param orderNum
     * @return
     */
    @Override
    public Boolean confirmPayStatus(String orderNum) {
        AlipayTradeQueryRequestBuilder builder = new AlipayTradeQueryRequestBuilder();
        builder.setOutTradeNo(orderNum);

        AlipayF2FQueryResult queryResult = alipayTradeService.queryTradeResult(builder);
        return queryResult.isTradeSuccess();
    }
}
